package com.webull.information.center.carwler.dao;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

import com.webull.information.center.carwler.common.model.NewsDetection;

@Repository
public class NewsDetectionDao {
	private static final RowMapper<NewsDetection.Detection> detection = BeanPropertyRowMapper
			.newInstance(NewsDetection.Detection.class);

	private @Autowired JdbcTemplate jdbcTemplate;
	private @Autowired NamedParameterJdbcTemplate npJdbcTemplate;

	public List<NewsDetection.Detection> queryAllDetection() {
		return jdbcTemplate.query(Sql.queryAllDetection, detection);
	}

	public List<NewsDetection.Detection> queryAllDetection(String source) {
		return jdbcTemplate.query(Sql.queryAllDetection + " and s_source= ?", new Object[] { source }, detection);
	}

	public Integer countByTickerUrl(Integer tickerId, String url_md5) {
		return jdbcTemplate.queryForObject(Sql.countByTickerUrl, Integer.class, tickerId, url_md5);
	}

	public void insertDetectionRes(NewsDetection.DetectionResult result) {
		npJdbcTemplate.update(Sql.insertResult, new BeanPropertySqlParameterSource(result));
	}

	private static class Sql {
		private static final String queryAllDetection = "select * from wli_news_detection where del = 0";
		private static final String countByTickerUrl = "select count(1) from wli_news_detection_result where del = 0 and ticker_id = ? and url_md5 = ?";
		private static final String insertResult = "insert into wli_news_detection_result(ticker_id,s_source,title,url,url_md5,news_time) values(:tickerId,:sSource,:title,:url,:urlMd5,:newsTime)";
	}
}
